import Koa from 'koa'
/**
 * 如果把 use(ctx => {
 *         ctx.status = 200;
 *         ctx.body = 'OK!';
 *     }) 放在前面，则`/sse`事件就得不到处理了
 */

new Koa()
    .use((ctx, next) => {
        // ctx: context, 常用单词中的辅音字母的组合表达该单词，另如：manager -> mgr; pointer -> ptr
        if (ctx.path !== '/')
            return next();
        ctx.status = 200
        ctx.body = 'OK!'
    })
    .use(async (ctx, next) => {
        if (ctx.path !== '/sse')
            return await next();

        // ctx.status = 200;
        ctx.type = 'text/event-stream'
        ctx.body = 'SSE!'
    })
    .listen(8080, _ => console.log('Listening on http://localhost:8080/sse'));
